﻿using System;
using System.Web.Mvc;
using System.Web.Security;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel.DataAnnotations;

using System.Web;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class AllowAnonymousAttribute : Attribute { }


public  class LogonRequired : AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {

        bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true)
            || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true);
      
      
          
            var name = HttpContext.Current.User.Identity.Name;
            if (!string.IsNullOrEmpty(name) || skipAuthorization == true)
            {
                FormsAuthentication.SetAuthCookie(name, true);
            }
            else
            {
                filterContext.Result = new HttpUnauthorizedResult();
            }

            base.OnAuthorization(filterContext);
    }


    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (HttpContext.Current.User != null)
        {
            return true;
        }
        return false;
    }
    
}


